<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/2
 * Time: 14:42
 * 评论
 */
namespace app\admin\controller;
use think\Db;
class Comment extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map = array();
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.comment|uuu.user_username'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($information_type)&&$information_type!="")
            {
                $map['a.type'] = $information_type;
            }

            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_comment')->alias('a')
                        ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                        ->where($map)
                        ->count();
            $lists = Db::table('up_comment')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->order('time desc')
                ->where($map)
                ->field('uuu.user_username,a.*')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            foreach ($lists as $key=>&$vo){
                    if($vo['type']==1){
                        $vo['type_name'] = '普通资讯';
                    }elseif ($vo['type']==8){
                        $vo['type_name'] = '自媒体资讯';
                    }elseif ($vo['type']==3){
                        $vo['type_name'] = '军婚军恋';
                    }elseif ($vo['type']==4){
                        $vo['type_name'] = '解读问答';
                    }elseif ($vo['type']==5){
                        $vo['type_name'] = '短视频';
                    }elseif ($vo['type']==6){
                        $vo['type_name'] = '悬赏问答';
                    }elseif ($vo['type']==7){
                        $vo['type_name'] = '故事分享';
                    }elseif ($vo['type']==9){
                        $vo['type_name'] = '考研升学论坛';
                    }elseif ($vo['type']==10){
                        $vo['type_name'] = '自训教官论坛';
                    }
                    $count1 = Db::table('up_comment_reply')->where('f_id', $vo['comment_id'])->count();
                    $vo['count'] = $count1;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?comment_id')){
            session('comment_id',null);
        }
        return $this->fetch();
    }

    public function del(){
        $id  = input('param.id');
        $where['comment_id'] = $id;
        $info1 = Db::table('up_comment_reply')->where('f_id',$id)->select();
        if($info1>0){
            Db::table('up_comment_reply')->where('f_id',$id)->delete();
        }
        $info = Db::table('up_comment')->where('comment_id',$id)->delete();
        if($info){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    //详情
    public function details()
    {
        $id  = input('param.id');
        $lists = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->order('time desc')
            ->field('uuu.user_username,a.*')
            ->where('comment_id',$id)
            ->find();//计算总页面
            if($lists['type']==1){
                $lists['type_name'] = '普通资讯';
            }elseif ($lists['type']==3){
                $lists['type_name'] = '军婚军恋';
            }elseif ($lists['type']==4){
                $lists['type_name'] = '解读问答';
            }elseif ($lists['type']==5){
                $lists['type_name'] = '短视频';
            }elseif($lists['type']==6){
                $lists['type_name'] = '悬赏问答';
            }elseif($lists['type']==7){
                $lists['type_name'] = '故事分享';
            }elseif($lists['type']==8){
                $lists['type_name'] = '自媒体资讯';
            }elseif($lists['type']==9){
                $lists['type_name'] = '考研升学论坛';
            }elseif($lists['type']==10){
                $lists['type_name'] = '自训教官论坛';
            }
        $this->assign('info',$lists);
        return $this->fetch();
    }
    //回复列表
    public function detail(){
        if(request()->isAjax()){
            $where = array();
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['a.f_id'] = session('comment_id');
            $count = Db::name('comment_reply')->alias('a')
                ->join('up_u_user uc','uc.user_id = a.user_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('comment_reply')->alias('a')
                ->join('up_u_user uc','uc.user_id = a.user_id','left')
                ->page($Nowpage,$limits)
                ->where($where)
                ->field('uc.user_username,a.*')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('param.id');
        if($id != ''){
            session('comment_id',$id);
        }
        return $this->fetch();
    }
    //删除回复列表
    public function delAnswer(){
        $id = input('param.id');
        $res = Db::table('up_comment_reply')->where('comment_reply_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}